package com.boc.langxiang.presenter.me;


import com.boc.langxiang.contract.me.MyExerciseJoinFragmentContract;
import com.boc.langxiang.entity.request.CanyulistRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.CanyuListBean;
import com.boc.langxiang.entity.response.CanyulistContent;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyExerciseJoinFragmentPresenter implements MyExerciseJoinFragmentContract.Presenter {

    private ApiService mService;
    private MyExerciseJoinFragmentContract.View mView;

    public MyExerciseJoinFragmentPresenter(MyExerciseJoinFragmentContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }


    @Override
    public void canyulist(String page) {
        mView.showLoading();
        CanyulistRequest canyulistRequest = new CanyulistRequest();
        canyulistRequest.page = page;
        Observable<BaseResponse<CanyulistContent>> canyulist = mService.canyulist(canyulistRequest.params());
        canyulist.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<CanyulistContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<CanyulistContent> response) {
                        CanyulistContent content = response.getContent();
                        if (content != null) {
                            List<CanyuListBean> list = content.getList();
                            int count = content.getCount();
                            mView.getCanyuList(list, count);
                        } else {
                            mView.getCanyuList(null, 0);
                        }

                    }
                });
    }

    @Override
    public void loadMore(String page) {
        mView.showLoading();
        CanyulistRequest canyulistRequest = new CanyulistRequest();
        canyulistRequest.page = page;
        Observable<BaseResponse<CanyulistContent>> canyulist = mService.canyulist(canyulistRequest.params());
        canyulist.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<CanyulistContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<CanyulistContent> response) {
                        CanyulistContent content = response.getContent();
                        List<CanyuListBean> list = content.getList();
                        int count = content.getCount();
                        mView.getLoadMoreList(list, count);
                    }
                });
    }
}
